package com.mcarthur.countdown;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Ball extends AbstractDrawable {
	public static final int WIDTH = 14;
	public static final int RADIUS = 7;

	private static final Color INACTIVE = new Color(221.f/255.f, 221.f/255.f, 221.f/255.f);
	
    protected IntBuffer   mVertexBuffer;
    private boolean active = false;
	private Color activeColor;
    
	public Ball(float posX, Color activeColor) {
		super(posX);
		setActiveColor(activeColor);
		
		int one = 0x10000;
		int vertices[] = { 0, 0, 0,
						   0, one, 0,
						   one, 0, 0,
						   one, one, 0 };

		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		mVertexBuffer = vbb.asIntBuffer();
		mVertexBuffer.put(vertices);
		mVertexBuffer.position(0);
    }

    public void draw(GL10 gl) {
    	gl.glMatrixMode(gl.GL_MODELVIEW);
    	gl.glPushMatrix();

		gl.glTranslatef(posX, posY, 0);
		gl.glScalef(WIDTH, WIDTH, 0);

		gl.glFrontFace(gl.GL_CW);
		
		if (active) {
			gl.glColor4f(activeColor.r, activeColor.g, activeColor.b, activeColor.a);
		} else {
			gl.glColor4f(INACTIVE.r, INACTIVE.g, INACTIVE.b, INACTIVE.a);
		}
		

		gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
		gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
		
		gl.glPopMatrix();
    }
    
    public void setActive(boolean active) {
		this.active = active;
	}
    
    public boolean isActive() {
    	return active;
    }
    
    public void setActiveColor(Color activeColor) {
    	this.activeColor = activeColor;
    }
    
    public Color getActiveColor() {
		return activeColor;
	}
}
